package xddq

import (
	"fmt"
	"math"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

func DestinySelfInfoDesc(info [3]*D.DestinySelfInfo) string {
	return fmt.Sprintf("[%s %s %s] 总魅力: %d", info[0].Name, info[1].Name, info[2].Name, info[0].Charm+info[1].Charm+info[2].Charm)
}

// 获取派遣任务仙友
func (xd *XdDriver) GetDestinySelfInfo(idx int) (bool, [3]*D.DestinySelfInfo) {
	count := 0
	var resInfo [3]*D.DestinySelfInfo
	for _, info := range xd.UserData.DestinySelfInfo {
		if info.Delegation > 0 {
			resInfo[count] = &info
			count++
		}
		if count >= 3 {
			if idx == 1 {
				idx = 0
				count = 0
				continue
			}
			return true, resInfo
		}
	}
	return false, resInfo
}

// 获取挑战任务仙友
func (xd *XdDriver) GetFightDestinySelfInfo(charmThreshold int64) (bool, [3]*D.DestinySelfInfo) {
	var resInfo [3]*D.DestinySelfInfo
	// 先过滤掉Fight=0的仙友
	var availableInfos []*D.DestinySelfInfo
	for i := range xd.UserData.DestinySelfInfo {
		if xd.UserData.DestinySelfInfo[i].Fight > 0 {
			availableInfos = append(availableInfos, &xd.UserData.DestinySelfInfo[i])
		}
	}
	n := len(availableInfos)
	if n < 3 {
		return false, resInfo
	}
	minDiff := int64(math.MaxInt64)
	var bestCombination [3]*D.DestinySelfInfo
	found := false
	// 三重循环遍历所有可能的3个仙友的组合
	for i := 0; i < n-2; i++ {
		for j := i + 1; j < n-1; j++ {
			for k := j + 1; k < n; k++ {
				sum := availableInfos[i].Charm + availableInfos[j].Charm + availableInfos[k].Charm
				if sum > charmThreshold {
					diff := sum - charmThreshold
					if diff < minDiff {
						minDiff = diff
						bestCombination = [3]*D.DestinySelfInfo{availableInfos[i], availableInfos[j], availableInfos[k]}
						found = true
					}
				}
			}
		}
	}
	if found && len(bestCombination[0].Name) > 0 && len(bestCombination[1].Name) > 0 && len(bestCombination[2].Name) > 0 {
		return true, bestCombination
	}
	// 如果没有找到满足条件的组合，返回Charm最大的三个可用仙友
	return true, [3]*D.DestinySelfInfo{availableInfos[0], availableInfos[1], availableInfos[2]}
}

// 处理瑶池仙踪信息
func (xd *XdDriver) CheckDestinyFightInfoData(allInfo D.DestinyFightInfo) {
	for _, task := range allInfo.Task {
		if task.Done {
			log.Debugf("瑶池仙踪-领取派遣任务%d", task.Idx+1)
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_GET_TASK_REWARD, "08"+task.ID))
			for i := 0; i < 5; i++ {
				time.Sleep(100 * time.Millisecond)
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_POOL_UP, ""))
			}
		}
	}
	exist, resInfo := xd.GetDestinySelfInfo(0)
	if !exist {
		log.Infof("瑶池仙踪-无派遣人员")
	}
	if exist && allInfo.TaskCount[0] == 0 {
		log.Infof("瑶池仙踪-派遣任务1 派遣: %s", DestinySelfInfoDesc(resInfo))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_GET_TASK,
			fmt.Sprintf("08 %s 08 %s 08 %s 10 00 18 05 20 00 28 01", resInfo[0].ID, resInfo[1].ID, resInfo[2].ID)))
	}
	exist, resInfo = xd.GetDestinySelfInfo(1)
	if !exist {
		log.Infof("瑶池仙踪-无派遣人员")
	}
	if exist && allInfo.TaskCount[1] == 0 {
		log.Infof("瑶池仙踪-派遣任务2 派遣: %s", DestinySelfInfoDesc(resInfo))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_GET_TASK,
			fmt.Sprintf("08 %s 08 %s 08 %s 10 00 18 05 20 00 28 01", resInfo[0].ID, resInfo[1].ID, resInfo[2].ID)))
	}
	if exist && len(xd.UserData.DestinySelfInfo) >= 18 && allInfo.TaskCount[2] == 0 {
		log.Infof("瑶池仙踪-派遣任务3 派遣: %s", DestinySelfInfoDesc(resInfo))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_GET_TASK,
			fmt.Sprintf("08 %s 08 %s 08 %s 10 00 18 05 20 00 28 01", resInfo[0].ID, resInfo[1].ID, resInfo[2].ID)))
	}
	if exist && len(xd.UserData.DestinySelfInfo) >= 24 && allInfo.TaskCount[3] == 0 {
		log.Infof("瑶池仙踪-派遣任务4 派遣: %s", DestinySelfInfoDesc(resInfo))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_GET_TASK,
			fmt.Sprintf("08 %s 08 %s 08 %s 10 00 18 05 20 00 28 01", resInfo[0].ID, resInfo[1].ID, resInfo[2].ID)))
	}
	xd.UserData.DestinyFightCount = allInfo.FightCount
	if allInfo.FightCount < 3 {
		log.Debugf("瑶池仙踪-查看挑战信息")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_ENTER_BATTLE, ""))
	}
}

// 处理瑶池仙踪挑战信息
func (xd *XdDriver) CheckDestinyFightListInfoData(allInfo D.DestinyFightListInfo) {
	fight := 0
	for _, info := range allInfo.Person {
		exist, resInfo := xd.GetFightDestinySelfInfo(info.Charm)
		if fight < 5-int(xd.UserData.DestinyFightCount) && exist {
			log.Debugf("瑶池仙踪-挑战%s,总魅力%d 我方派遣: %s", info.Name, info.Charm, DestinySelfInfoDesc(resInfo))
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_BATTLE,
				fmt.Sprintf("08 %s 18 %s 18 %s 18 %s 20 01", info.TaskID, resInfo[0].ID, resInfo[1].ID, resInfo[2].ID)))
			resInfo[0].Fight--
			resInfo[1].Fight--
			resInfo[2].Fight--
			fight++
			continue
		}
		break
	}
	//log.Debugf("瑶池仙踪-刷新一次")
	//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_DESTINY_FIGHT_BATTLE_LIST, "08 01"))
}

// 处理瑶池仙踪仙友信息
func (xd *XdDriver) CheckDestinySelfInfoData(allInfo []D.DestinySelfInfo) {
	xd.UserData.DestinySelfInfo = allInfo
	flag := false
	for _, info := range allInfo {
		if info.Delegation > 0 || info.Fight > 0 {
			log.Infof("%+v", info)
			if !flag {
				flag = true
				xd.ResetTaskByID(proto.S_DESTINY_FIGHT_DESTINY_LIST)
				xd.ResetTaskByID(proto.S_DESTINY_FIGHT_ENTER_BATTLE)
			}
		}
	}
}
